package com.example.thread.demo;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName VolatileExample.java
 * @Description TODO
 * @createTime 2021年01月13日 15:25:00
 */
public class VolatileExample {

    private static boolean flag = false;
    private static Integer i = 0;

    @Test
    public void test01() {
        new Thread(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
                flag = true;
                System.out.println("子线程修改flag为true");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        while (!flag) {
            i++;
//            System.out.println("flag=" + flag);
//            try {
//                TimeUnit.MILLISECONDS.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
        System.out.println("程序结束，i=" + i);
    }

    @Test
    public void test02() {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
    }
}
